#! /bin/bash

### 	WARNING THIS SCRIPT IS FOR SUPERUSER ONLY###

#  NVIDIA-MAKER v0.6
#  Sat Feb  6 16:58:20 2010
#  Copyright  2010  mk67
#  <rmk677@gmail.com>
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with main.c;if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
SHELL=/bin/bash
PATH=/usr/bin:/bin:/usr/local/bin
SCRIPT_VS="0.66"
SCRIPT_NM="nvidia-maker"
# export PATH=${PATH}:XX

## special dev only, DON'T EDIT, COMMENT OR UNCOMMENT'
#locale="$nvdir/locale"
#EXEC="/home/mike/"
EXEC=''
locale="home/mike/Dev/svn/nvidia-maker-installer/"

cd /
### VARS
## Master Vars.
nvdir="/usr/local/src64/NVIDIA"
sourcedir="/usr/local/src64"
nvtar="$nvdir/tgz"					# archies directory
nvtmp="$nvdir/temp"					# install temp directory
nvlog="$nvdir/log"					# logs directory
nvdl="$nvdir/release"					# downlaod driver backups directory
nvupdate="$nvdir/update"				# update temp directory
logfile="--log-file-name=$nvlog/install.log"		# nvidia-installer options
temp="--tmpdir=$nvtmp"					# nvidia-installer option: install temp dir
dltemp="--tmpdir=$nvupdate"				# nvidia-installer option: update temp dir
kernel="-kernel-install-path"
kernel_src="--kernel-source-path"
# dev control only do not edit
if [[ $(ls $locale | grep "PACK") != '' ]]; then
	lang_dir=$locale
else
	lang_dir=$nvdir
fi
# language packs PUT LANG PACKS IN NVIDIA PATH
FR_PACK=$(ls $lang_dir | grep "FR")
EN_PACK=$(ls $lang_dir | grep "EN")
## Script colours Vars.
t='\e[0;39m'	#default terminal colour
r='\e[1;31m'	#red
v='\e[1;32m'	#green
y='\e[1;36m'	#cyan
j='\e[1;33m'	#yellow
cBl='\e[40m\e[1m\e[37m'	#darkgray Bold gray
eN='\e[0m'  			#Normal characters
eB='\e[1m'  			#Bold
eU='\e[4m'  			#underlined
eBl='\e[5m' 			#Blinking
eBB='\e[1m\e[5m'		#Bold & Blink
eBBr='\e[1m\e[5m\e[7m'	#Bold & Blink & reverse
eR='\e[7m'  			#Reverse video
eBr='\e[1m\e[7m'		#Bold & reverse

### FUNCTIONS
ID(){
## card id
board=$(lspci -nn | grep 'VGA' | awk '{ print $9" "$10" "$11" "$12" "$13 }' | tr '[[]]' ' ')
pci=$(lspci -nn | grep 'VGA' | awk '{ print $1 }')
device=$(lspci -n | grep "$pci" | cut -d ':' -f 4 | awk '{ print $1 }')
## vendor
vendor=$(lspci -nn | grep 'VGA' | awk '{ print  $1" "$6" "$7 }')
user=$(whoami)
}
user_id(){
	if [[ $(whoami) != "root" ]] ; then
		echo -e "\t\t$v##$j$eBB              YOU ARE NOT ROOT$eN              $v##"
		echo -e "\t\t##  NEXT PROMPT WILL ASK YOU FOR SU PASSWORD  ##"
		echo -e "\t\t##    IF YOU DON'T WANT THIS, PRESS CTRL+C    ##$j"
		if [ -f /proc/version ] ; then
			distro_list=$(echo -e "ubuntu\ndebian\nfedora\nredhat\nmandriva")
			for distro in $distro_list ; do
				d1_version=$(cat /proc/version | grep $distro)
				if [[ $d1_version != '' ]] ; then
					d1_version=$distro
					if [[ $(echo $d1_version | grep "ubuntu\|debian") != '' ]] ; then
						SU="sudo"
					else
						SU="su -c"
					fi
				fi
			done
				#$SU "bash -x $EXEC$0 $OP1 $OP2"
				$SU "$EXEC$0 $OP1 $OP2"
				clear
			exit 0
		else
			echo -e "$v## SORRY, CAN'T IDENTIFY DISTRO. PROMPT AS SU ##"
			echo -e "##  AND TYPE $eB sudo $(basename $0)$eN$v FOR DEBIAN LIKE   ##"
			echo -e "##    OR$eB su -c $(basename $0)$eN$v FOR OTHER DISTRO.     ##$t\n"
			exit 0
		fi
	fi
}
## system arch
arch(){
ARCH=`uname -a | awk '{ print $12 }'`
if [ $ARCH == "i686" ] || [ $ARCH == "i586" ] ; then
	ARCH="x86"
	RUN_PCK="pkg1"
else
	RUN_PCK="pkg2"
fi
}
## system distro --> need progress
distro(){
if [ -f /proc/version ] ; then
	d2_version="cat /proc/version"
	cat /proc/version | grep ubuntu > /dev/null
	if [[ $? -eq 0 ]]; then
		DISTRO="UBUNTU"
		PKG_INSTALLER="apt-get install"
		d2_version=$( uname -r | cut -d '-' -f 1 )
		PKG="lftp kernel-headers-$d2_version gcc xterm gksu"
		X="Xorg"
	fi
	cat /proc/version | grep mandriva > /dev/null
	if [[ $? -eq 0 ]]; then
		DISTRO="MANDRIVA"
		PKG_INSTALLER="urpmi"
		desk=$( uname -r | cut -d '-' -f 2 )
		kernel=$( uname -r | cut -d '-' -f 1 )
		d2_version=$( uname -r | cut -d '-' -f 3 )
		PKG="lftp kernel-$desk-devel-$kernel-$d2_version gcc xterm gksu"
		X="X"
	fi
else
	echo -e "\f$r $msg101 " ###
fi
}
# dependencies control
dep_control(){
	ls /usr/bin/lftp > /dev/null
	if [ $? -ne 0 ] ; then
		echo -e "$v Required dependencies are not installed "; sleep 2 ###
		echo -e "$y INSTALL RUNNING..." ###
		$PKG_INSTALLER $PKG
		echo -e "$y INSTALL DONE   " ###
	fi
}
# Internet connection control function.
router_ip(){
	wget -O - -q icanhazip.com
}
ping_net(){
if [[ "$(ping -q -c 1 nvidia.com | sed -n '4p' | awk '{print $1}')" == 1 ]] ; then
	connection="$msg116"
else
	connection="$msg117"
fi
}
ip_net(){
	#question="eth0,eth1,ppp0,ppp1,wlan0,wlan1"
	question=$(/sbin/ifconfig | sed -n '/lo/d;/^ /g;/ $/p' | awk '{print $1}')
	#echo "$question" | tr ',' '\n' > $EXEC/net_list
	for net in $question ; do #$(cat $EXEC/net_list) ; do

		if [ "$(/sbin/ifconfig $net | sed -n '/lo/d;/^ /g;/ $/p' | awk '{ print $1 }')" != '' ] ; then
		#ip_check=$(  )
			if [[ "$(/sbin/ifconfig $net | awk '{ print $2 }' | sed -n 2p | cut -d ':' -f 2 | grep [[:digit:]])" != '' ]]; then
				net_ip=$(/sbin/ifconfig $net | awk '{ print $2 }' | sed -n 2p | cut -d ':' -f 2)
				if [[ $(router_ip) != $net_ip ]] ; then
					ip=$(router_ip)
				else
					ip=$net_ip
				fi
				iface=$net
			fi
		fi
	done
#		net=$net_check
#		ip=$ip_check
}
# Primary control of X and network connection.
xlocksession(){
	if [ -e /tmp/.X0-lock ] ; then				# TEST >> if [[ `runlevel | awk '{print $2}'` == 5 ]] ; then
		echo -e "\f$y  $msg104$t\f"
		#echo -e "\f$v  $msg105 \n$msg114\n$msg115$t\f"
		echo -e "\f$v $msg107$j $qst001 $v:\n ==> $j\c"
		read -n 1 wilu ; echo -e "$v"
		if [ "$wilu" == "$ans001" ] ; then
		echo -e "$y \f $msg108"
		echo -e "$v\r\\t $qst003 " ### question 003
			read -s -n 1 confirm
			if [ $confirm == Q ] ; then
				menu
			else
				if [ $confirm == C ] ; then
				echo -e "\t $msg110 ...." ; sleep 1
				dm_stop
				#/usr/sbin/gdm-stop && init 3 && exec /home/mike/$0
				fi
			fi
		else
			menu
		fi
		#sleep 1 ; exit 1
		#menu
	fi
}
connection_control(){
	ip_net
	if [[ "$(echo $ip)" == 127.* ]] || [[ "$(echo $ip)" == '' ]]; then
		echo -e "$r  $msg106$t\f"
	fi
}
# making links and originals libraries
rmlink(){
	librmlk(){
		rm -vf $class/libGL.*
		rm -vf $class/libGLcore.*
		rm -vf $class/libOpenCL.*
		rm -vf $class/libcuda.*
		rm -vf $class/libXvMCNVIDIA.*
		rm -vf $class/libnvidia-cfg.*
		rm -vf $class/libnvidia-tls.*
		rm -vf $class/libnvidia-compiler.*
		rm -vf $class/libvdpau.so.*
		rm -vf $class/libvdpau_nvidia.so.*
		rm -vf $class/libvdpau_trace.so.*
		rm -vf $class/vdpau/libvdpau_nvidia.so.*
		rm -vf $class/vdpau/libvdpau_trace.so.*
		rm -vf $class/tls/libnvidia-tls.*
	}
	xorgrmlk(){
		rm -vf $class/xorg/modules/libnvidia-wfb.*
		rm -vf $class/xorg/modules/extensions/libglx.*
	}
	tlsrmlk(){
		rm -vf $class/tls/libnvidia-tls.*
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ $(uname -a |grep -o "64"|sed -n '1p') -eq 64 ]] ; then
		#for lib64 in rmlink ; do
		class=$lib64
		librmlk ; xorgrmlk ; tlsrmlk
		#done
		#for lib32 in rmlink ; do
		class=$lib32
		librmlk ; tlsrmlk
		#done
	else
		if [[ $(uname -a |grep -o "64"|sed -n '1p') != 64 ]] ; then
			class=$lib32
			librmlk ; xorgrmlk ; tlsrmlk
		fi
	fi
}
# Interanl archive links creation
makelink(){
	usrlink(){
		cd $class
		ln -s -f libGL.so.* libGL.so.1
		ln -s -f libGL.so.1 libGL.so
		ln -s -f libGLcore.so.* libGLcore.so.1
		ln -s -f libOpenCL.so.* libOpenCL.so.1.0
		ln -s -f libOpenCL.so.1.0 libOpenCL.so.1
		ln -s -f libOpenCL.so.1 libOpenCL.so
		ln -s -f libcuda.so.* libcuda.so.1
		ln -s -f libcuda.so.1 libcuda.so
		ln -s -f libnvidia-cfg.so.* libnvidia-cfg.so.1
		ln -s -f libnvidia-cfg.so.1 libnvidia-cfg.so
		ln -s -f libnvidia-tls.so.* libnvidia-tls.so.1
		#ln -s -f tls/libnvidia-tls.so.* tls/libnvidia-tls.so.1
		ln -s -f libnvidia-compiler.so.* libnvidia-compiler.so.1
		ln -s -f libnvidia-compiler.so.1 libnvidia-compiler.so
		ln -s -f libvdpau.so.* libvdpau.so.1
		ln -s -f libvdpau.so.1 libvdpau.so
		ln -s -f vdpau/libvdpau_nvidia.so.* libvdpau_nvidia.so
		ln -s -f vdpau/libvdpau_trace.so.* libvdpau_trace.so
		ln -s -f libXvMCNVIDIA.so.* libXvMCNVIDIA_dynamic.so.1
		ln -s -f libXvMCNVIDIA_dynamic.so.1 libXvMCNVIDIA_dynamic.so
		cd vdpau
		ln -s -f libvdpau_nvidia.so.* libvdpau_nvidia.so.1
		ln -s -f libvdpau_trace.so.* libvdpau_trace.so.1
		cd /
	}
	xorglink(){
		cd $class/xorg/modules
		ln -s -f libnvidia-wfb.so.* libnvidia-wfb.so.1
		cd extensions
		#ln -s -f xorg/modules/libnvidia-wfb.so.1 xorg/modules/libwfb.so
		ln -s -f libglx.so.* libglx.so
		cd /
	}
	tlslink(){
		cd $class/tls
		ln -s -f libnvidia-tls.so.* libnvidia-tls.so.1
		cd /
	}
	binlink(){
		cd /usr/bin
		ln -s -f nvidia_installer nvidia-uninstall
		cd /
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ $(uname -a |grep -o "64"|sed -n '1p') -eq 64 ]] ; then
		class=$lib64
		usrlink ; tlslink ; xorglink ; binlink ; sleep 1
		class=$lib32
		usrlink ; tlslink ; binlink ; sleep 1
	else
		if [[ $(uname -a |grep -o "64"|sed -n '1p') -ne 64 ]] ; then
			class=$lib32
			usrlink ; tlslink ; xorglink ; binlink ; sleep 1
		fi
	fi
}
# Directories and files listing.
list(){
		#listdir="$tmplist"
		wlist=`ls -d $listdir$opt1$opt2$opt3 | sed 's|'$optlist'|--->  |' | cat -n`
		echo "$wlist"
		echo -e "\r$v $ans002\n ==>  $j\c"
		read list
		result=`echo "$wlist" | sed -n ''$list'p'`
		#echo ${result#*--> }
}
## Some Other Vars.
# Check Installed version
# elf type
libclass(){
	if [[ "$(uname -a |grep -o "64"|sed -n '1p')" == "64" ]] ; then
		ELF_TYPE="64"
	else
		ELF_TYPE=""
	fi
}
version_id(){
	libclass
	if [ -f $nvlog/version.log ]||[ -f $nvlog/install.log ]; then
		if [ -f $nvlog/version.log ]; then
			version=$(cat $nvlog/version.log)
		elif [ -f $nvlog/install.log ]; then
			version=$(cat $nvlog/install.log|grep " version:"|sed -n 's/^.*: //p')
		fi
	else
		version=$(echo /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.* | sed -n 's/^.*so.//p')
	fi
}
# check aviable updates
check_update(){
COMPAT(){
echo -e "\n$v $msg207 ..."
if [[ $LAST_DRV == $LAST_BETA ]] ; then
		DIFF=$LAST_DRV
		DVS(){
			TF=1
			VS="$msg212 & $msg213"
		}
else
		DIFF=$(echo -e "$LAST_DRV\n$LAST_BETA")
		DVS(){
			if [[ $DRV == $LAST_DRV ]] ; then
				TF=1
				VS=$msg212
			else
			#if [[ $DRV == $LAST_BETA ]] ; then
				TF=2
				VS=$msg213
			fi
		}
fi
for DRV in $DIFF ; do
	DVS
	wget -q -O $nvtmp/compat.$TF ftp://download.nvidia.com/XFree86/Linux-$ARCH/$DRV/README/supportedchips.html
	DEVICE=$(cat $nvtmp/compat.$TF | sed -n "/<td>0x$device<\/td>/p" | sed -n 's/<[^>]*//g;s/>//g;p' )
	VDPAU=$(cat $nvtmp/compat.$TF | sed -n "/<td>0x$device<\/td>/{n;p}" | sed -n 's/<[^>]*//g;s/>//g;p' )
	if [[ $DEVICE != '' ]] ; then
		if [[ $VDPAU != '' ]] ; then
			if [[ $VDPAU == "-" ]] ; then
				ACCEL_V="$msg208"
			else
				ACCEL_V="$msg209 $y$VDPAU$v."
			fi
		fi
		echo -e "\n\t$v$y$board$v $ACCEL_V"
		echo -e "\t$v $j$DRV$v $VS $msg210."
	else
		echo -e "\n\t$v $j$DRV$v $VS $msg211."
	fi
done
}
echo -e "\n$v $msg205 ..."
ping_net
connection_control
if [[ "$(echo $ip)" == 127.* ]] || [[ "$(echo $ip)" == '' ]]; then
	echo -e "\f$r $msg411$connection"
	echo -e "$v $msg418\n $msg417$t"
	menu
else
	if [[ $net_ip != $ip ]] ; then
		echo -e "\f$v $msg111$connection $msg118 $j$( echo $iface )$v.\n $msg119 $msg112:\t$j$(echo $ip)$v\n $msg113 $msg112:\t$j$(echo $net_ip)$v.\n"
	else
		echo -e "\f$v $msg111$connection $msg118 $j$( echo $iface )$v.\n $msg113 $msg112:\t$j$(echo $ip)$v.\n"
	fi
	echo -e "$v $msg206 ...$y\n"
	lftp -c "anon; cd ftp://download.nvidia.com/XFree86/Linux-$ARCH/ ; ls > $nvtmp/drvlist ; cat latest.txt > $nvtmp/last_update " >/dev/null
	LAST_IN=$version
	LAST_DRV=$(cat $nvtmp/last_update | awk '{ print $1 }')
	LAST_BETA=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -gr | sed -n 1p)
	echo -e "\n\t$v $msg201$y$board$v:"
	echo -e "\n\t$v $msg202\t\t$j$LAST_IN "
	echo -e "\n\t$v $msg203\t\t$j$LAST_DRV "
	echo -e "\n\t$v $msg204\t\t\t$j$LAST_BETA $v"
	COMPAT
fi
}
#goto_install(){
#	echo -e "\f$v $msg214$j $qst001 $v:\n ==> $j\c"
#	read -n 1 y_n ; echo -e "$v"
#	if [ "$y_n" == "$ans001" ] ; then
#		xlocksession
#		from_net
#	else
#		menu
#	fi
#}

### INSTALLATION
### ARCHIVED DRIVERS INSTALL FUNCTION.
from_archive(){
	echo -e "$v $msg220$y\r"
	cd $nvtar
		listdir="$nvtar"
		opt1="/*"
		optlist="${listdir#*}"
	list
	drivertar=${result#*-->  /}
	echo -e "\n$v $msg221$j ${result#*-->  /}."
	echo -e "\n$r --->$y     $msg222$r     <---$y\r"
	cd /
	echo -e "\n$j $msg223...$y"
	rmlink
	cp -T `tar xvf $nvtar/$drivertar` /
	echo -e "\n$j $msg224... $y\n"
	makelink
	libclass
	if [ -f /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$version ] ; then
		msg00A=$(ls -l /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$version | cut -d ' ' -f 6,7 | sed -n "s/ / $(echo $msg231) /p")
		msg00B=$(ls /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$version | cut -d '/' -f 7 | sed -n 's/[[:lower:]]//g;s/..//p')
		echo -e "\n$r >>$v $msg225$r<<"
		echo -e "\n$v $msg232 $msg00B $msg230 $msg00A"
	else
		echo -e "\n$v >>$r $msg226$v<<"
		echo -e "\n$v $msg227 "
		menu
	fi
	echo -e "\n$v $msg228"
	echo -e "$v $qst002\n$j $qst1$r ==>$j \c"
	read -n 1 dmrestart
		if [ $dmrestart == "$ans001" ] ; then
			echo -e "$v $msg229\r" ; sleep 2
			modprobe -r nvidia ; depmod -qa
			modprobe nvidia ; dm_restart
			#break
		else
			echo -e "$v\n" ; menu
		fi
}

### FROM A USER DIRECTORY INSTALL FUNCTION.
from_directory(){
	installdrv(){
		if [[ -s $driverun ]] ; then
			echo -e "\n$r --->$v     $msg301$r     <---$y\r"
			sh $driverun -a -n -q --no-cc-version-check --ui=none $temp $logfile
			echo -e "$j $msg302\n$y" ; sleep 2
			cd $nvtmp ; sh $driverep -x
			nvlv=$($nvdir/nvidia-installer --latest --ui=none | sed -n '5p' | awk '{ print $4 }')
			#drv_actu=`ls -D | grep '${nvlv%;*}'`		# liste les Répertoire NVIDIA présent
			if [ -x $driverun ] ; then
				#cp -f $driverrep/nvidia-installer $nvdir
				cp -f $driverun $nvdl/nv-update-${nvlv%;*}
				if [[ -f $nvdl/nv-update-${nvlv%;*} ]] ; then
					echo -e "\n$v $msg303$j ${nvlv%;*}$v $msg305\,\n $msg304.\n"
					sleep 1
				else
					echo -e "\n$v $msg303$j ${nvlv%;*}$v $msg306."
					echo -e "\n$v $msg307"
					echo -e "\n$v $msg309"
					echo -e "\n$v $msg309"
					read -n 1 ok
				fi
			fi
			archivage
		else
			echo -e "\n$v $msg310 \n"
			echo -e "\n$v $msg311$y http://www.nvidia.fr/Download/Find.aspx?lang=en$v $msg316,"
			echo -e "\n$v $msg311$y ftp://download.nvidia.com/XFree86/"
			echo -e "\n$v $msg312$t\n"
			echo -e "\n$v $msg313 $j"
			select answer in "$msg314" "$msg315" ; do
				case $answer in
					"$msg314")
						menu
						;;
					"$msg315")
						dm_restart
						;;
				esac
			done
		fi
	}
	echo -e "$v $msg320$y\n"
	ls $nvdl
	echo -e "\n$v $msg321$j\n"
		VP=" $msg322"
		RU=" $msg323"
	select repo in "$VP" "$RU" ; do
		case $repo in
			"$VP")
				echo -e "$y" ; cd $nvdl
				echo -e "$v $msg324 $nvdl : $y"
				listdir="$nvdl"
				opt1="/*"
				opt3=""
				optlist="${listdir#*}"
				list
				driver="${result#*-->  /}"
				driverun="$nvdl/$driver" ; installdrv
			;;
			"$RU")
				# niveau primaire
				echo -e "$y" ; cd /home
				listdir="/home"
				opt1="/*"
				opt3="/"
				optlist="${listdir#}"
				list
				homerep="${result#*-->  $opt3}" ; echo $homerep
				cd $homerep
				echo -e "\n$v $msg325$j $homerep$y," ; sleep 1
				ls  -d /home/$homerep/* | sed 's|/home/'${homerep#*}'|--->  |'
				echo -e "\n$v $msg326"
				echo -e "$v ==> $j\c"
				read -e driver
				driverun="/home/$homerep/$driver"
				chmod a+x $driverep
				installdrv
			;;
		esac
	done
}

goto_install(){
	echo -e "\f$v $msg215$v:$j\r"
	PS3=$( echo -e "\n$v $ans002$j ==>")
	select todo in "$A" "$E" "$F" ; do
		case $todo in
			"$A")
				from_net
			;;
			"$E")
				echo -e "\n$y $msg401"
				download_only
			;;
			"$F")
				menu
			;;
	#		"$I")
	#			exit 0
	#		;;
		esac
	done
}
download_only(){
	if [ -e $nvdir/nvidia-installer ] ; then
		cd $nvupdate
		echo -e "$v\n $msg413$j\r"
		A1="$LAST_DRV"
		A2="$LAST_BETA"
		A3="$msg419"
		PS3="`echo -e "\n$v $ans002$j ==>"` "
		select choice4 in "$A1" "$A2" "$A3"; do
			case $choice4 in
				"$A1")
					echo -e "$v--->  $msg414...  <---\r"
					LAST_PACK=$LAST_DRV
					last_pack
				;;
				"$A2")
					echo -e "$v--->  $msg415...  <---\r"
					LAST_PACK=$LAST_BETA
					last_pack
				;;
				"$A3")
					echo -e "$v--->  $msg413...  <---\r"
					package_list
					LAST_PACK=$PICK_DRV
					last_pack
				;;
			esac
			if [ -f $nvupdate/$RUN_PACK ]; then
				echo -e "\f$v $msg402\n $msg528"
				mv -f $nvupdate/$RUN_PACK $nvdl/nv-update-$LAST_PACK
				chmod 755 $nvupdate/nv-update-$LAST_PACK
				menu
			else
				echo -e "$v\f$msg406 $j$RUN_PACK$v $msg407\n $msg528"
				menu
			fi
		done
	fi
}
### UPDATE FUNCTION, FROM INTERNET.
package_list(){
	pck_drv=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -g | sed -n 's/^/--> /p' | cat -n)
	echo -e "$y$pck_drv"
	echo -e "\n$v $msg420"
	echo -e "$v $ans002\n ==>  $j\c"
	read pck
	PICK_DRV=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -g | sed -n ''$pck'p')
	echo -e "\n$v $msg421 $j$PICK_DRV" ; sleep 1
}
last_pack(){
	echo -e "\n$v $msg405 ...$j\n"
	lftp -c "anon; cd ftp://download.nvidia.com/XFree86/Linux-$ARCH/$LAST_PACK/ ; ls > $nvtmp/bug_list ; quit"
	if [ "$(cat $nvtmp/bug_list | grep -w "$RUN_PCK")" == '' ] ; then
		echo -e "$v $msg406 $j$RUN_PCK$v $msg407, $msg408 $j$LAST_PACK$v $msg409:$j"
		cat -n $nvtmp/bug_list | awk '{ print $1") "$10 }' | grep -w "run" ; echo -e "$v ==> $j\c"
		read -n 1 VERSION
		RUN_PACK=$(cat -n $nvtmp/bug_list | awk '{ print $10 }' | sed -n "$VERSION"p)
	else
		RUN_PACK=$(cat -n $nvtmp/bug_list | awk '{ print $10 }' | grep -w "$RUN_PCK")
	fi
	clear
	echo -e "\n$r --->$v     $msg410$j $RUN_PACK$r.$y"
	echo -e "\n$r --->$v     $msg401$r ...$y\r" # ; echo -e "$r prossessing $RUN_PACK..." ; sleep 2
	wget -c --progress=bar:force ftp://download.nvidia.com/XFree86/Linux-$ARCH/$LAST_PACK/$RUN_PACK $nvupdate/
	echo -e "\n$r --->$v     $msg402$r $y\r" ; sleep 1
}
installe(){
	echo -e "\n$r --->$v     $msg403$r $y\r" ; sleep 1
	nwdrv=$(ls $nvupdate | grep "$LAST_PACK") ; echo "$nwdrv"
	sh $nwdrv -a -q -n --ui=none $dltemp $logfile
	# pre achiving
	if [[ $(ls $nvupdate/| grep "$nwdrv") != '' ]] ; then
		sh $nvupdate/$nwdrv -x
		cd $nvdir
	else
		echo -e "\n$j $msg404\n "
		sleep 1 ; menu
	fi
	drvrep=$(ls $nvtmp/ | grep "$LAST_PACK")		# NVIDIA directory listing
	#cp -f $nvupdate/$drvrep/nvidia-installer $nvdir
	mv -T $nvupdate/$nwdrv $nvdl/nv-update-$LAST_PACK
	mv -f $nvupdate/* $nvtmp/
	cp -f $nvtmp/$drvrep/nvidia-installer $nvdir
	chmod 755 $nvtmp/*
}
from_net(){
# download functions
	if [ -e $nvdir/nvidia-installer ] ; then
		cd $nvupdate
		# le binaire Nvidia à un peu de mal avec l'exec après transfert,
		# aidons-le un peu.
		#check_update
		echo -e "\n\t$v $msg412\n\t $msg422\n\t $msg423"
			read -s -n 1 confirm2
			if [ $confirm2 == C ] ; then
				echo -e "$v\n $msg413$j\r"
				A1="$LAST_DRV"
				A2="$LAST_BETA"
				A3="$msg419"
				PS3="`echo -e "\n$v $ans002$j ==>"` "
				select choice4 in "$A1" "$A2" "$A3"; do
					case $choice4 in
						"$A1")
							echo -e "$v--->  $msg414...  <---\r"
							LAST_PACK=$LAST_DRV
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
						"$A2")
							echo -e "$v--->  $msg415...  <---\r"
							LAST_PACK=$LAST_BETA
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
						"$A3")
							echo -e "$v--->  $msg413...  <---\r"
							package_list
							LAST_PACK=$PICK_DRV
							last_pack
							installe
							#rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
					esac
				done
			else
				if [ $confirm2 == M ] ; then
					rm -f $nvtmp/drvlist $nvtmp/last_up
					sleep 1 ; menu
				fi
			fi
	else
		echo -e "$v $msg416"
		sleep 1 ; $B
	fi
}
### GOODIES
upgrade_new_kernel(){
	echo -e "$v $msg611: "
	echo -e "$y" ; cd /lib/modules/
	listdir="/lib/modules"
	opt1="/*"
	opt4="/"
	optlist="${listdir#}"
	list
	kernel_rep="${result#*-->  $opt4}" ; echo -e "\n$j $kernel_rep"
	NEW_KERNEL=$kernel_rep
	upgrade_kernel
	menu
}
upgrade_kernel(){
	if [[ $(echo $NEW_KERNEL) != '' ]];then
		KERNEL=$NEW_KERNEL
	else
		KERNEL=$(uname -r)
	fi
	kernel="--kernel-name=$KERNEL"
	kernel_mods="--kernel-install-path=/lib/modules/$KERNEL/"
	kernel_src="--kernel-source-path=/usr/src/linux-$KERNEL"
	drv_release=$(ls $nvdl/ | grep "$version")
	echo -e "$v $msg612 $KERNEL ...$y"
	cd $nvdl/
	$nvdl/$drv_release -a -n --ui=none $temp $kernel_src $kernel_mods -K $temp $logfile
	echo -e "$v $msg613."
}
### ARCHIVING. ###
drivercall(){
	ls $nvtmp | grep "$version"
}
# sub function: METHOD CHOICE
archivage(){
echo -e "$v $msg501\n $qst002$j $qst001$v:"
	echo -e "\n$v ==>$j \c" ; read -n 1 archive
	case $archive in
		"$ans001")
			echo -e "\n$v $msg502..." ; sleep 1
			echo -e "$y $(ls -d $nvtmp/$(drivercall))"
			cd $nvtmp #$nvdl/nv-update-$LAST_PACK
			if [[ $(ls $nvtmp | grep "$version") != '' ]] ; then
				echo -e "\n$v\"$j "$(ls $nvtmp | grep "$version")"  $v\" $msg503\n$y"
			else
				echo -e "\n$v $msg504"
				sleep 1 ; menu
			fi
			archive ; sleep 1
			echo -e "$v\n  $msg505" ; sleep 1
	 		echo -e "$v\n $msg506\n"
			rm -f -R $nvtmp/*
			sleep 2 ; archivage "$ans003"
		;;
		"$ans003")
			rm -f -R $nvtmp/*
			echo -e "$v\n $msg507" ; sleep 1
			echo -e "$v\n $msg508"
			echo -e "$v\n $msg509\n"
			sleep 2 ; menu
		;;
	esac
}

### REAL ARCHIVING
archive(){
cd $nvtmp
## archive function
 	do_tar(){
		if [[ -d $nvtmp/$nvrep ]] ; then
			echo -e "\n$r>>$j $msg511 $version. $r <<$y\n" ; sleep 2
		else
			echo -e "\n$r $msg512"
			echo -e "\n$v $msg513$y\n" ; sleep 1
			rm -f -R $nvtmp/* ; menu
		fi
		tmpdir=$version
		mkdir  -v -p $tmpdir
		if [ -d $nvtmp/$tmpdir ] && [ -d $nvtmp/$nvrep ] ; then
				mkdir -p $tmpdir/lib/modules/`uname -r`/kernel/drivers/video
				mkdir -p $tmpdir/usr/lib
				mkdir -p $tmpdir/usr/lib/tls
				mkdir -p $tmpdir/usr/lib/vdpau
				mkdir -p $tmpdir/usr/bin
				mkdir -p $tmpdir/usr/include
				mkdir -p $tmpdir/usr/share
				mkdir -p $tmpdir/etc
				if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" == '' ] ; then
					mkdir -p $tmpdir/etc/OpenCL/vendors
					mkdir -p $tmpdir/usr/share/applications
					mkdir -p $tmpdir/usr/share/doc
					mkdir -p $tmpdir/usr/share/doc/html
					mkdir -p $tmpdir/usr/share/man/man1
					mkdir -p $tmpdir/usr/share/pixmaps
					mkdir -p $tmpdir/usr/include/GL
					mkdir -p $tmpdir/usr/include/CL
					mkdir -p $tmpdir/usr/include/vdpau
					mkdir -p $tmpdir/usr/include/cuda
				fi
				cp -f /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko $tmpdir/lib/modules/`uname -r`/kernel/drivers/video/
				if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" != '' ] ; then
					cp -R $nvrep/etc/* $tmpdir/etc/
					cp $nvrep/usr/bin/* $tmpdir/usr/bin/
					cp -R $nvrep/usr/include/* $tmpdir/usr/include/
					cp -R $nvrep/usr/share/* $tmpdir/usr/share/
				else
					cp -f $nvrep/*.icd $tmpdir/etc/OpenCL/vendors/
					mv $nvrep/nvidia*[.*z] $tmpdir/usr/share/man/man1/
					mv $nvrep/*.desktop $tmpdir/usr/share/applications/
					cp $nvrep/[[:upper:]]* $tmpdir/usr/share/doc/
					cp $nvrep/html/*.html $tmpdir/usr/share/doc/html/
					mv $nvrep/*.png $tmpdir/usr/share/pixmaps/
					cp $nvrep/nvidia-* $tmpdir/usr/bin/
					cp $nvrep/ma* $tmpdir/usr/bin/
					cp $nvrep/mk* $tmpdir/usr/bin/
					cp $nvrep/tls_* $tmpdir/usr/bin/
					cp $nvrep/cl*h $tmpdir/usr/include/CL/
					cp $nvrep/gl*h $tmpdir/usr/include/GL/
					cp $nvrep/cu*h $tmpdir/usr/include/cuda/
					cp $nvrep/vd*h $tmpdir/usr/include/vdpau/
				fi
				if [[ "$(uname -a | grep -o "64"|sed -n '1p')" == 64 ]] ; then
					mkdir -p $tmpdir/usr/lib64/xorg/modules
					mkdir -p $tmpdir/usr/lib64/tls
					mkdir -p $tmpdir/usr/lib64/vdpau
					if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" == '' ] ; then
						mkdir -p $tmpdir/usr/lib64/xorg/modules/drivers
						mkdir -p $tmpdir/usr/lib64/xorg/modules/extensions
					fi
					if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" != '' ] ; then
						cp  $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib64/
						cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib64/xorg/modules/
						cp  $nvrep/usr/lib/* $tmpdir/usr/lib64/
						cp  $nvrep/usr/lib/tls/* $tmpdir/usr/lib64/tls/
						cp  $nvrep/usr/lib/vdpau/* $tmpdir/usr/lib64/vdpau/
						cp  $nvrep/usr/lib32/* $tmpdir/usr/lib/
						cp  $nvrep/usr/lib32/tls/* $tmpdir/usr/lib/tls/
						cp  $nvrep/usr/lib32/vdpau/* $tmpdir/usr/lib/vdpau/
					else
						cp  $nvrep/*drv* $tmpdir/usr/lib64/xorg/modules/drivers/
						mv  $nvrep/libglx.so.* $tmpdir/usr/lib64/xorg/modules/extensions/
						cp  $nvrep/lib*wfb* $tmpdir/usr/lib64/xorg/modules/
						mv  $nvrep/*vd*nvidia* $tmpdir/usr/lib64/vdpau/
						mv  $nvrep/*vd*trace* $tmpdir/usr/lib64/vdpau/
						cp  $nvrep/lib*so* $tmpdir/usr/lib64/
						cp  $nvrep/lib*[.*a] $tmpdir/usr/lib64/
						cp  $nvrep/tls/* $tmpdir/usr/lib64/tls/
						cp  $nvrep/32/tls/* $tmpdir/usr/lib/tls/
						mv  $nvrep/32/*vd*nvidia* $tmpdir/usr/lib/vdpau
						mv  $nvrep/32/*vd*trace* $tmpdir/usr/lib/vdpau
						cp  $nvrep/32/lib* $tmpdir/usr/lib/
					fi
				else
					mkdir -p $tmpdir/usr/lib/xorg/modules
					if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" == '' ] ; then
						mkdir -p $tmpdir/usr/lib/xorg/modules/drivers
						mkdir -p $tmpdir/usr/lib/xorg/modules/extensions
					fi
					if [ "$(cat $nvrep/pkg-history.txt | grep -w "$RUN_PCK")" != '' ] ; then
						cp  $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib/
						cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib/xorg/modules/
						cp  $nvrep/usr/lib/* $tmpdir/usr/lib/
						cp  $nvrep/usr/lib/tls/* $tmpdir/usr/lib/tls/
						cp  $nvrep/usr/lib/vdpau/* $tmpdir/usr/lib/vdpau/
					else
						cp  $nvrep/*drv* $tmpdir/usr/lib/xorg/modules/drivers/
						cp  $nvrep/libglx.so.* $tmpdir/usr/lib/xorg/modules/extensions/
						cp  $nvrep/lib*wfb* $tmpdir/usr/lib/xorg/modules/
						mv  $nvrep/*vd*nvidia* $tmpdir/usr/lib/vdpau/
						mv  $nvrep/*vd*trace* $tmpdir/usr/lib/vdpau/
						cp  $nvrep/lib*so* $tmpdir/usr/lib/
						cp  $nvrep/lib*[.*a] $tmpdir/usr/lib/
						cp  $nvrep/tls/* $tmpdir/usr/lib/tls/
					fi
				fi
			cd $tmpdir ; tar cvf $nvtar/$tmpdir-`uname -r`.tar.gz ./etc ./lib ./usr
			cd $nvdir ; rm -R $nvtmp/*
			echo -e "\n$v  $msg514$j $tmpdir-$(uname -r)"
			sleep 1
		else
			echo -e "$v $msg515 "
			echo -e " $msg516"
			rm -f -R $nvtmp/* ; menu
		fi
	}
echo -e " $(drivercall)"
# actual driver version before pkg run extraction
	if [ -f $nvdl/nv-update-$version ] ; then
		pkg=$(ls $nvdl | grep "$version")
		echo -e "$y $msg521$j $version."
		#echo -e "$y $msg522$j ${pkg#*te-}.$y"
		if [[ -d $(drivercall) ]] ; then
			echo -e "$r $msg523"
			sleep 1
		else
			echo -e "$r $msg524"
			sh $nvdl/$pkg -x
		fi
		nvrepls=$(ls $nvtmp | grep "NVIDIA")
		nvrep=$(echo ${nvrepls%/})
		ls -v $nvtmp | grep "$version"
			if [ -d $nvtmp/$nvrep ]; then
				echo -e "\f$r>>$y $nvrep ready ! $r <<$y\n" ; sleep 2
			else
				echo -e "\f$j $nvrep $msg525$y\n" ; sleep 1
				rm -f -R $nvtmp/* ; menu
			fi
		do_tar
		rm -f -R $nvtmp/* ; menu
		menu
	else
	    echo -e "$y $msg526$j $version$y $mqg527$v\n"
	    echo -e "$v $msg528\n"
	    rm -f -R $nvtmp/* ; menu
	fi
}

### RESTART X SESSION
drv_load(){
    /sbin/modprobe -r nvidia
    /sbin/depmod -qa
    /sbin/modprobe nvidia
}
dm_restart(){
	if [[ -e /tmp/.X0-lock ]] ; then
	    if [[ -e /var/run/gdm.pid ]] ; then
			/usr/sbin/gdm-stop ; /usr/sbin/gdm
	    else
			killall -9 gdm-binary ; /usr/sbin/gdm
	    fi
	else
	    drv_load
	    /sbin/init 5 && /usr/sbin/gdm
	fi
}
dm_stop(){
	if [[ -e /tmp/.X0-lock ]] ; then
	    if [[ -e /var/run/gdm.pid ]] ; then
			/usr/sbin/gdm-stop && /sbin/telinit 3
	    else
			killall -9 $X && /sbin/telinit 3
	    fi
	else
	    /sbin/init S
	fi

}
### UNINSTALL fUnction
uninstall(){
	echo -e "$v $qst002 $j$qst001$v :"
	echo -e "\n$v ==>$j \c"
	read -n 1 yn
	if [ "$yn" == "$qst004" ] ; then
		xlocksession
		echo -e "\n$r --->$y     $msg531$r     $y\r"
		cd $nvdir
		./nvidia-installer --uninstall --ui=none $dltemp $logfile
		echo -e "\f$v $msg532 \n" ; sleep 2
		menu
	else
		menu_install
	fi
}
config(){
	#
	echo
}
script_update(){
	for update_me in $(echo -e $chk_up|tr ',' '\n'|grep -w "$chk_vs") ; do
	if [[ $(echo $update_me | grep -w "$SCRIPT_NM-$chk_vs\|$PACK-$chk_vs") != '' ]] ; then
		echo -e "$v$eB $msg722$eN$v ..."
		wget -q -O $nvdir/$update_me http://the-nvidia-swiss-knife.googlecode.com/files/$update_me
		chmod 755 $nvdir/$update_me
		ln -s -f $nvdir/$update_me /usr/local/bin/$SCRIPT_NM
	else
		if [[ $(echo $update_me | grep -w "$SCRIPT_NM-$SCRIPT_VS\|$PACK-$SCRIPT_VS") == '' ]] ; then
		#if [[ $(echo $update_me | grep -w "($SCRIPT_NM\|$PACK)-[$SCRIPT_VS]") != '' ]] ; then
			echo -e "$r$eBl*$eN$v$eBr  $msg723  $eN$eBl$r*$eN$v"
			menu
		fi
	fi
	done
	#ln -s -f $nvdir/$update_me /usr/local/bin/$SCRIPT_NM.sh
	#if [[ $(echo $update_me | grep -w "$SCRIPT_NM") != '' ]] ; then
	#	update_vs=$(echo $update_me | sed -n 's/^.*-//;s/.sh//p')
	#	sed -ni "s/SCRIPT_VS=\"\(.*\)\"/SCRIPT_VS=\"$update_vs\"/i;p" $EXEC$0
	#fi
}
script_version(){
	wget -q -O update_look http://code.google.com/p/the-nvidia-swiss-knife/downloads/list
	#check_up=$(cat update_look|grep "href=\"detail?name="|grep -v  "cancelBubble"| sed -n 's/^.*name=//g;s/&amp.*$//g;p')
	chk_up=$(cat update_look | perl -0777 -pe 's/\<[^\>]+\>//gs;s/body.*\}//gs;s/&nbsp\;//gs;s/&diams\;//gs'|\
	sed -n 's/Sort\|Hide\|Show\|Up\|Down\|Column//g;p'|sed -n '/^[ ]*$/d;/loadCount/,/Filename/p'|sed -n 's/^ //g;p'|tr '\n' ',')
	#for chk_name in $check_up ; do
	if [[ $(echo -e $chk_up|tr ',' '\n'|grep -w "$SCRIPT_NM") != '' ]] ; then
		if [[ $(echo -e $chk_up |tr ',' '\n'|grep -o "$SCRIPT_VS"|sed -n '$p') == '' ]] ; then
			chk_name=$(echo -e $chk_up|tr ',' '\n'|grep -w "$SCRIPT_NM"|sed -n '$p')
			echo -e "\f$r$eBl*$eN$v$eBr  $msg720 $chk_name $msg721  $eN$r$eBl*$eN$v\n"
			chk_vs=$(echo -e $chk_up|tr ',' '\n'|grep -w "$SCRIPT_NM"|sed -n 's/[a-z]\|-//g;s/.$//p'|sed -n '$p')
		fi
	fi
	#done
}
lang_define(){
		if [ "$LG" == "EN" ] || [ "$OP2" == "EN" ] ; then
				PACK=$EN_PACK
		elif [ "$LG" == "FR" ] || [ "$OP2" == "FR" ] ; then
				PACK=$FR_PACK
		fi
	## language pack
	. $locale$PACK
}
title(){
	#echo -e "\f\t$j $msg701 \r"
	#echo -e "\t$j $msg702 \f" ; sleep 1
	#echo -e "\f$v $msg703\r"
	#echo -e " $msg704 \r"
	#echo -e " $msg705\n"
	echo -e "\f\f\t\t\t$eB$y¤¤   ¤ ¤     ¤ ¤ ¤¤¤¤   ¤   ¤¤    "
	echo -e       "\t\t\t¤¤   ¤ ¤     ¤ ¤ ¤   ¤  ¤  ¤  ¤   "
	echo -e       "\t\t\t¤ ¤  ¤  ¤   ¤  ¤ ¤    ¤ ¤ ¤    ¤  "
	echo -e       "\t\t\t¤  ¤ ¤   ¤ ¤   ¤ ¤   ¤  ¤ ¤¤¤¤¤¤  "
	echo -e       "\t\t\t¤   ¤¤    ¤    ¤ ¤¤¤¤   ¤ ¤    ¤ ®\f"
	echo -e "\t\t\t$eN$r   **$j $msg701$r **        $v"
	sleep 2 ; clear
	#$1 ; $2
}
root_id(){
	if [[ $user != 'root' ]] ; then
		user_id
		#echo -e "\f\f\t\t\t$r$msg702 \n\t\t$j$msg703.$t"
		#sleep 2 ; exit 0
	fi
}
dev_messages(){
	echo -e "$v  $msg706\t\t\t $j$DISTRO$v \n  $msg707\t\t $j$ARCH$v \n  $msg708\t\t\t$j$board "
	echo -e "$v  $msg724\t $j"$(cat $PWD/$0 | grep "SCRIPT_VS=\"*\""|sed -n 's/[a-z]\|[=_"]//g;p')" $v \n"
	echo -e "$v  $msg202\t$j $version \n"
	echo -e "$v  $msg709\n"
}
install_controls(){
	if [ -d $nvdir ] ; then
		mkdir -p $nvtar $nvtmp $nvlog $nvupdate $nvdl $locale
	fi
	nvdl_last=$(ls -l $nvdl/* | grep -v "\-rwxr\-xr\-x"|awk '{print $8}')
	if [[ $nvdl_last != '' ]] ; then
		chmod 755 $nvdl_last
	fi
	dep_control
	if [[ $(ls $sourcedir/ | grep "NVIDIA") != '' ]] ; then
		echo -e "$v OK. $msg710" #; ls $nvdir
		#xlocksession
	else
		if [[ $(ls $sourcedir/ | grep "NVIDIA") == '' ]] ; then
			echo -e "$y\f  $msg711\f"
			echo -e "$v  $msg712"
			echo -e "$v  $msg713\n   $y==>$j \c"
			read -e sourcedir
			mkdir -p $sourcedir/NVIDIA
			query_dir=$(echo $sourcedir | sed -n 's/\//\\\//g;p')
			sed -ni "s/nvdir=\"\(.*\)\"/nvdir=\"$query_dir\/NVIDIA\"/i;p" $EXEC$0
			sed -ni "s/sourcedir=\"\(.*\)\"/sourcedir=\"$query_dir\"/i;p" $EXEC$0
		fi
	fi
}

### SUB MENU
menu_config(){
	clear ; echo -e "$r NOT YET" ; sleep 1 ; menu_manage
	echo -e "$v\n $C:$j\r"
	PS3=$(echo -e "\n$v $ans002$j ==>")
	select config in "$C1" "$C2" "$C3" "$C4" "$C5" "$M" ; do
		case $config in
			"$C1") # single
				clear ; echo -e "$v--->"

			;;
			"$C2B") # crt
				clear ; echo -e "$v--->"
			;;
			"$C3") # twin
				clear ; echo -e "\n$r--->"

			;;
			"$C4") # dual
				clear ; echo -e "$v--->"
			;;
			"$C5") # test > single > crt > dual > twin
				clear ; echo -e "$v--->"
			;;
			"$M") # back to menu
				clear ; menu_manage
			;;
		esac
	done
}
menu_install(){
	clear
	echo -e "$v\n $A:$j\r"
	PS3=$(echo -e "\n$v $ans002$j ==>")
	select installe in "$A1" "$A2" "$A3" "$B2" "$M" ; do
		case $installe in
			"$A1")
				clear ; echo -e "$v--->  $msg602  <---\r" ; xlocksession ; from_archive
			;;
			"$A2")
				clear ; echo -e "$v--->  $msg603  <---\r" ; xlocksession ; from_directory
			;;
			"$A3")
				clear ; echo -e "\n$r--->$v  $msg604$r  <---$v\n" ; xlocksession
				check_update ; from_net
			;;
			"$B2")
				clear ; echo -e "$v--->  $msg606 <---\r" ; uninstall
			;;
			"$M") # back to menu
				clear ; menu
			;;
		esac
	done
}
menu_manage(){
	clear
	echo -e "$v\n $B:$j\r"
	PS3=$(echo -e "\n$v $ans002$j ==>")
	select manage in "$B1" "$B4" "$B5" "$B6" "$C" "$D" "$M"; do
		case $manage in
			"$B1")
				clear ; echo -e "\n$r--->$v  $msg605$r <---\n$y" ; archive
			;;
			"$B4")
				clear
				echo -e "$v $msg608 "
				sleep 2 ; dm_restart ; exit 0
			;;
			"$B5")
				clear ; echo -e "$v$msg609 ... \f"
				check_update ; goto_install ; menu #; rm -f $nvtmp/drvlist  $nvtmp/last_up
			;;
			"$B6")
				clear ; echo -e "$v$msg615 ... \f"
				script_update ; menu #; rm -f $nvtmp/drvlist  $nvtmp/last_up
			;;
			"$C") # xconfig
				menu_config
			;;
			"$D")
				#clear ; echo -e "$r NOT YET" ; sleep 1 ; menu_manage
				nvidia-settings
			;;
			"$M") # back to menu
				clear ; menu
			;;
		esac
	done
}
menu_goodies(){
	clear
	echo -e "$v\n $G:$j\r"
	PS3=$(echo -e "\n$v $ans002$j ==>")
	select goodies in "$G1" "$G2" "$M" ; do
		case $goodies in
			"$G1")
				clear
				xlocksession
				echo -e "\n$r--->$v  $msg614$r \n"
				upgrade_kernel
				menu
			;;
			"$G2")
				clear
				xlocksession
				echo -e "\n$r--->$v $msg610 \n"
				upgrade_new_kernel
				menu
			;;
			"$M") # back to menu
				clear ; menu
			;;
		esac
	done
}
### MAIN MENU
menu(){
echo -e "$v\n $msg601$j\r"
PS3=$( echo -e "\f$v NOTE:\n $OP.\n$v $NP." ; echo -e "\n$v $ans002$j ==>")
select choice in "$A" "$B" "$G" "$B3" "$B4" "$H" "$I" ; do
	case $choice in
		"$A")
			menu_install
		;;
		"$B")
			menu_manage
		;;
		"$G")
			menu_goodies
		;;
		"$B4")
			clear
			echo -e "$v $msg608 "
			sleep 2 ; dm_restart ; exit 0
		;;
		"$B3")
			clear ; echo -e "$v $msg607 "
			#sleep 2 ; /usr/sbin/gdm-stop ; exit 1
			sleep 2 ; dm_stop ; exit 1
		;;
		"$I") # quit
			echo -e "$t\n" ; exit 0
		;;
		"$H") # help
			clear ; echo -e "$r NOT YET" ; sleep 1 ; menu
		;;
	esac
done
}

perl_default(){
ID
arch
lang_define
root_id
#script_version
distro
version_id
#dev_messages
}
first_start(){
lang_define
### FIRST START
title
root_id
script_version
## Creation of necessary directories in case they aren't already there.:
distro
version_id
dev_messages
install_controls
menu
}
### SCRIPT INTRO
while getopts "x-t-A-L-I-D-K-N-T-m-u-s-c-d-h" OPTS ; do
	if [ "$2" == "EN" ] ; then
		LG="EN"
	elif [ "$2" == "FR" ] ; then
		LG="FR"
	else
		echo -e "$r no language pack chosen\n EN = english\n FR = Français.$t"
		exit 0
	fi
	OP1=$(echo $1)
	OP2=$(echo $2)
	case $OPTS in
		x)
			GEO=82x28
			xterm -sb -b 5 -bg black -bd black -bw 0 -geometry $GEO -e $EXEC$0 -t $LG ##
			#xterm -sb -b 5 -bg blue -bd black -bw 0 -geometry $GEO -e $EXEC$0 -t $LG ##
		;;
		t)
			ID ; arch ; first_start
		;;
		# XC)
		#	opts2 ; ID ; arch ; first_start
		#;;
		h)
			echo -e "$(basename $0)$msg801" #"nvidia_maker_install.sh usage :
			echo -e "$msg802" #\n\t -t, --terminal \t exec installer in terminal mode."
			echo -e "$msg803" #"\t -x, --xterm_ui \t exec in a pretty xterm interface. \n\t -h, --help \t\t this help."
			echo -e "$msg804"
			exit 1
		;;
		c) # function test command (debug only)
			#ARCH="x86_64"
			#LG=FR
			#version_id
			#echo -e "$y $version $t"
			#echo -e "\f$v $msg111 $(echo $ip) $msg112 $(echo $iface) $msg113 \n$t"
		;;
		d) # function test command (debug only)
			#ARCH="x86_64" ; LG=FR ; RUN_PCK="pkg2"
			#nvdl=/usr/local/src64/NVIDIA/release
			#version=256.29
			#upgrade_
			#new_kernel
			#echo -e "$y$connection$t"
			exit 0
		;;
		A)
			echo "Archive install"
			pl_default ; from_archive
		;;
		I)
			echo "Serveur install"
			pl_default; xlocksession
			check_update ; from_net
		;;
		L)
			echo "Local Install"
			pl_default ; from_directory
		;;
		D)
			echo "Dé_install" # *
			pl_default; uninstall
		;;
		K)
			echo "kernel" # *
			pl_default; upgrade_kernel
		;;
		N)
			echo "nouveau kernel" # *
			pl_default; upgrade_new_kernel
		;;
		T)
			echo "faire une archive" # *
			pl_default; archive
		;;
		u)
			echo "controle update" # *
			pl_default; check_update
		;;
		s)
			echo "update script" # *
			pl_default; script_update
		;;
		m)
			pl_default; dev_messages
		;;
		*)
			$EXEC$0 -h
			exit 0
		;;
	esac
done
exit 0
